package test;

import static org.junit.Assert.*;

import main.FacadeDispatcher;
import main.FacadeTime;
import main.FacadeUnit;
import main.World;
import movement.Location;

import org.junit.Before;
import org.junit.Test;

import unit.Garage;
import unit.PoliceCar;
import unit.Unit;
import dispatch.DispatchCenter;
import emergency.*;

public class useCaseTestIndicateEndOfTask {
	FacadeUnit facade1;
	FacadeDispatcher facade2;
	FacadeTime facade3;
	Location justALocation;
	
	DispatchCenter center;
	Garage garage;
	
	Emergency em1, em2, em3, em4;
	Unit u1,u2,u3;
	
	
	@Before
	public void setUp() throws Exception {
		center = new DispatchCenter("DC", new Location(0,0,"DCC"),new Location(5,0,"G"));
		garage = center.getGarage();
		
		String args[] = new String[1];
		args[0] = "environment.dat";
		World world = new World(args);
		world.setDispatchCenter(center);
		
		facade1 = new FacadeUnit(world);
		facade2 = new FacadeDispatcher(world);
		facade3 = new FacadeTime(world);
		
		garage = center.getGarage();
		justALocation = new Location(10,10,"NameDoesn'tMatter");
		
		em4 = new Robbery(justALocation, Severity.NORMAL, center, true, true);
		
		u1 = new PoliceCar("P1",100,center.getGarage());
		u2 = new PoliceCar("P2",120,center.getGarage());
		u2.setCurrentLocation(new Location(999,999,"nl"));
		u3 = new PoliceCar("P3",130,center.getGarage());
	}
	
	@Test
	public void testIndicateEndOfTask() throws Exception{
		assertFalse(u1.isAssigned());
		assertFalse(u2.isAssigned());
		assertFalse(u3.isAssigned());
		
		assertEquals(em4.getStateName(),"staterecorded");
		
		facade2.assignAllUnits(em4);
		
		assertTrue(u1.isAssigned());
		assertTrue(u2.isAssigned());
		assertTrue(u3.isAssigned());
		assertEquals(em4.getStateName(),"stateinprogress");
		
		facade3.simulatedTimeBy(10000);
		// all units are on emergency => can be finished!
		
		facade1.finishEmergency(u1);
		facade1.finishEmergency(u2);
		facade1.finishEmergency(u3);
		
		assertEquals(em4.getStateName(),"statecompleted");
	}
	
	@Test (expected=IllegalArgumentException.class)
	public void testIndicateEndOfTaskNotAllOnEmergency() throws Exception{
		assertFalse(u1.isAssigned());
		assertFalse(u2.isAssigned());
		assertFalse(u3.isAssigned());
		
		assertEquals(em4.getStateName(),"staterecorded");
		
		facade2.assignAllUnits(em4);
		
		assertTrue(u1.isAssigned());
		assertTrue(u2.isAssigned());
		assertTrue(u3.isAssigned());
		assertEquals(em4.getStateName(),"stateinprogress");
		
		facade3.simulatedTimeBy(10);
		// all units are on emergency => can be finished!
		
		facade1.finishEmergency(u1);
		facade1.finishEmergency(u2);
		facade1.finishEmergency(u3);
		
	}
}
